import {Component, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges} from '@angular/core';

@Component({
  selector: 'app-select-time',
  templateUrl: './datepicker-library.component.html',
  styleUrls: ['./datepicker-library.component.scss']
})

export class DatepickerLibraryComponent implements OnInit, OnChanges, OnDestroy {

  @Input() granularity: number = 1;        // 时间控件中分钟显示的粒度值，譬如值为15时 ，控件中分钟选项显示的为0,15，30，45
  @Input() granularityType: string;   // 粒度类型，只有在普通控件不清楚粒度的单位才需要设置，只可选MIN和H
  @Input() type: string;                // 类型：月=MON，天=D，分钟=M
  @Input() delay: number = 0;      // 往前倒推的时间，天粒度则表示往前推的天数，分钟粒度则往前倒推分钟数
  @Input() delayType: string;     // 倒推类型，只有在普通控件不清楚倒推的单位才需要设置
  @Input() theme: string = '';         // 颜色主题，可选值为'theme1' | 'theme2' | 'theme3'
  @Input() isDisabled: boolean = true;    // 是否禁选此刻以后日期
  @Input() dateFormat: string;          // 显示时间的格式
  @Input() isInit: boolean;    // 是否默认初始化时间控件
  @Input() initDate: string;     // 初始化日期
  @Input() placeHolder = '请选择日期';    // 输入框提示文字
  @Input() showTime: boolean = true;    // 是否显示时间选择功能
  @Input() relative: boolean = false;   // 是否双组件相关联
  @Input() returnParam: string = '';   // 返回给调用组件的参数
  @Input() rtDateFormat: string = '';   // 返回时间格式
  @Input() startDate: Date;             // 双组件的开始时间
  @Input() endDate: Date;               // 双组件的结束时间
  @Output() selectDate = new EventEmitter();    // 选中时间后返回事件

  date: any;
  curDate: any = new Date();
  initialTime: Date = new Date();
  curQuarter = 'Q1';
  quarterIndex = 0;
  quarterList = ['Q1', 'Q2', 'Q3', 'Q4'];
  quarterOpen = false;
  quarterWinEvent;
  disabledDateTime: any;
  disabledDate: any;
  showDateTime: any;
  prompt: any;
  format: string;
  initial: boolean;
  refreshTime: number;
  refreshInterval = null;

  constructor() { }

  ngOnInit() {
    this.quarterWinEvent = (e) => {          // 季度全局点击事件
      const targetPath = e['path'];
      const datePickerWrap = targetPath.find(node => {
        if (node.classList) {
          return node.classList.contains('quarter') || node.classList.contains('ant-calendar');
        }
      });
      if (!datePickerWrap) {
        this.quarterOpen = false;
      }
    };
    window.addEventListener('click', this.quarterWinEvent);
  }

  ngOnChanges(changes: SimpleChanges) {
    for (const propName of Object.keys(changes)) {
      if (propName === 'type') {
        this.startDate = null;
        this.endDate = null;
        this.validateGranularity();
        this.initDatePicker();
      }

      if (propName === 'relative') {
        this.startDate = null;
        this.endDate = null;
        this.initDatePicker();
      }

      if (propName === 'granularity') {
        this.validateGranularity();
        this.initDatePicker();
      }

      if (propName === 'delay') {
        this.delay = Number(this.delay);
        this.initDatePicker();
      }

      if (propName === 'startDate') {
        if (this.startDate) {
          this.startDate = new Date(this.startDate);
        } else if (this.initDate) {
          this.startDate = new Date(this.initDate);
        }
        this.setDisabledDate();
      }

      if (propName === 'endDate') {
        if (this.endDate) {
          this.endDate = new Date(this.endDate);
        } else if (this.initDate) {
          this.endDate = new Date(this.initDate);
        }
        this.setDisabledDate();
      }
    }
  }

  ngOnDestroy() {
    window.removeEventListener('click', this.quarterWinEvent);
    if (this.refreshInterval) {
      clearInterval(this.refreshInterval);
    }
  }

  // 校验粒度值是否有效
  validateGranularity() {
    const type = this.delayType || this.type;
    this.granularity = Number(this.granularity);

    if (type === 'MIN' && (this.granularity < 1 || this.granularity > 30)) {
      this.granularity = 1;
    }
    if (type === 'H' && (this.granularity < 1 || this.granularity > 12)) {
      this.granularity = 1;
    }
  }

  // 计算刷新时间
  countRefreshTime(step) {
    switch (this.type) {
      case 'D':
        this.refreshTime = 1000 * 60 * 60 * 24 * step;
        break;

      case 'H':
        this.refreshTime = 1000 * 60 * 60 * step;
        break;

      case 'MIN':
        this.refreshTime = 1000 * 60 * step;
        break;
    }

    this.refreshInterval = setInterval(() => {
      this.resetDatePicker();
    }, this.refreshTime);
  }

  // 自动刷新更新时间控件
  resetDatePicker() {
    this.date = new Date();
    this.initialTime = new Date();
    this.initDatePicker();
    this.openCalendar();
  }

  // 往前倒推 delay 个年或月的第一天0点时间戳
  setDelayFirstDay(date) {
    // setDate(0) 设置为上一个月的最后一天
    // setMonth(-1) 设置为上一年的最后一月
    for (let i = 0; i < this.delay; i++) {
      this.type === 'Y' ? date.setMonth(-1) : date.setDate(0);
    }
    if (this.type === 'Y') {
      date.setMonth(0);
    }
    date.setDate(1);
    date.setHours(0, 0, 0, 0);
  }

  // 初始化时间控件
  initDatePicker() {
    // 设置输入框提示文字
    this.setPlaceHolder();

    // 设置时间格式化
    this.setDateFormat();

    // 设置是否显示时间选择
    this.setShowDateTime();

    if (this.isInit !== undefined) {
      this.initial = this.isInit;
    } else {
      this.type === 'R' ? this.initial = false : this.initial = true;
    }

    this.initialTime = this.initDate ? new Date(this.initDate) : new Date();
    if (this.type !== 'C') {
      this.granularityType = null;
      this.delayType = null;
    }

    // 当前时间减去倒推时间的差值
    const dateDiffMin = new Date(this.initialTime.getTime() - 1000 * 60 * this.delay);
    const dateDiffHour = new Date(this.initialTime.getTime() - 1000 * 60 * 60 * this.delay);
    const diffMin = dateDiffMin.getMinutes();
    const diffHour = dateDiffHour.getHours();

    switch (this.granularityType || this.type) {
      case 'MIN':
        this.initialTime = new Date(dateDiffMin.getTime() - 1000 * 60 * (diffMin - (diffMin - diffMin % this.granularity)));
        break;
      case 'H':
        this.initialTime = new Date(dateDiffHour.getTime() - 1000 * 60 * 60 * (diffHour - (diffHour - diffHour % this.granularity)));
        break;
      case 'D':
        this.initialTime.setDate(this.initialTime.getDate() - this.delay);
        break;
    }

    if (this.isDisabled) {
      this.setDisabledDate();
    } else {
      this.disabledDate = null;
      this.disabledDateTime = null;
    }

    // 需要时间初始化
    if (this.initial) {
      this.curDate = this.initDate ? new Date(this.initDate) : new Date();
      this.date = this.initDate ? new Date(this.initDate) : new Date();

      switch (this.delayType || this.type) {
        case 'MON':
        case 'Y':
          if (this.delayType === 'MON') {
            this.curDate = new Date(this.curDate.setMonth(this.curDate.getMonth() - this.delay));
            this.date = new Date(this.date.setMonth(this.date.getMonth() - this.delay));
          } else if (this.delayType === 'Y') {
            this.curDate = new Date(this.curDate.setFullYear(this.curDate.getFullYear() - this.delay));
            this.date = new Date(this.date.setFullYear(this.date.getFullYear() - this.delay));
          } else {
            this.setDelayFirstDay(this.curDate);
            this.setDelayFirstDay(this.date);
          }
          break;
        case 'D':
          this.curDate.setDate(this.curDate.getDate() - this.delay);
          this.date.setDate(this.date.getDate() - this.delay);
          break;
        case 'H':
          this.curDate = new Date(dateDiffHour.getTime() - 1000 * 60 * 60 * (diffHour % this.granularity));
          this.date = new Date(dateDiffHour.getTime() - 1000 * 60 * 60 * (diffHour % this.granularity));
          break;
        case 'MIN':
          this.curDate = new Date(dateDiffMin.getTime() - 1000 * 60 * (diffMin % this.granularity));
          this.date = new Date(dateDiffMin.getTime() - 1000 * 60 * (diffMin % this.granularity));
          break;
        case 'R':
          this.curDate = [new Date(), new Date()];
          this.date = [new Date(), new Date()];
          break;
      }

      if (!this.initDate) {
        this.returnPickerParam();
      }
    } else {
      this.curDate = null;
      this.date = null;
    }
  }

  // 设置输入框提示文字
  setPlaceHolder() {
    if (this.placeHolder) {
      this.prompt = this.placeHolder;
    } else {
      // 设置时间显示格式与输入框提示文字
      switch (this.type) {
        case 'H':
          this.prompt = '请选择小时';
          break;

        case 'MON':
          this.prompt = '请选择月份';
          break;

        case 'Y':
          this.prompt = '请选择年份';
          break;

        case 'R':
          this.prompt = ['开始日期', '结束日期'];
          break;

        default:
          this.prompt = '请选择日期';
          break;
      }
    }
  }

  // 设置时间格式化
  setDateFormat() {
    if (this.dateFormat) {
      this.format = this.dateFormat;
    } else {
      switch (this.type) {
        case 'C':
          this.format = 'yyyy-MM-dd HH:mm:ss';
          break;

        case 'MIN':
          this.format = 'yyyy-MM-dd HH:mm';
          break;

        case 'H':
          this.format = 'yyyy-MM-dd HH';
          break;

        case 'D':
          this.format = 'yyyy-MM-dd';
          break;

        case 'W':
          this.format = 'yyyy-MM-dd';
          break;

        case 'MON':
          this.format = 'yyyy-MM';
          break;

        case 'Q':
          this.format = 'yyyy-MM-dd';
          break;

        case 'Y':
          this.format = 'yyyy';
          break;

        case 'R':
          this.format = 'yyyy-MM-dd HH:mm:ss';
          break;
      }
    }
  }

  // 设置是否显示时间选择
  setShowDateTime() {
    switch (this.type) {
      case 'C':
        if (this.showTime) {
          this.showDateTime = {
            nzFormat: 'HH:mm:ss'
          };
          this.granularityType === 'MIN' ? this.showDateTime['nzMinuteStep'] = this.granularity : this.showDateTime['nzHourStep'] = this.granularity;
        } else {
          this.showDateTime = false;
        }
        break;
      case 'MIN':
        this.showDateTime = {
          nzFormat: 'HH:mm',
          nzMinuteStep: this.granularity
        };
        break;

      case 'H':
        this.showDateTime = {
          nzFormat: 'HH',
          nzHourStep: this.granularity
        };
        break;

      case 'D':
        this.showDateTime = false;
        break;

      case 'MON':
        this.showDateTime = false;
        break;

      case 'Y':
        this.showDateTime = false;
        break;

      default:
        if (this.showTime) {
          this.showDateTime = {
            nzFormat: 'HH:mm:ss'
          };
        } else {
          this.showDateTime = false;
        }
    }
  }

  // 设置禁选日期
  setDisabledDate() {
    const incomeDate = new Date(this.startDate || this.endDate);
    const delayType = this.delayType || this.type;

    // 设置禁选日期
    switch (this.type) {
      case 'MON':
        this.disabledDate = (current: Date): boolean => {
          const today = new Date();
          const totalTodayMonth = today.getFullYear() * 12 + today.getMonth();
          const totalCurrentMonth = current.getFullYear() * 12 + current.getMonth();
          let totalIncomeMonth = 0;

          if (incomeDate) {
            totalIncomeMonth = incomeDate.getFullYear() * 12 + incomeDate.getMonth();
          }

          if (this.endDate) {
            return totalIncomeMonth - totalCurrentMonth < this.delay;
          } else if (this.startDate) {
            return this.isDisabled ? ((totalIncomeMonth - totalCurrentMonth >= this.delay || totalTodayMonth - totalCurrentMonth < this.delay)) : (totalIncomeMonth - totalCurrentMonth > this.delay);
          } else {
            return totalTodayMonth - totalCurrentMonth < this.delay;
          }
        };
        break;

      case 'Y':
        this.disabledDate = (current: Date): boolean => {
          const today = new Date();
          const totalTodayYear = today.getFullYear();
          const totalCurrentYear = current.getFullYear();
          let totalIncomeYear = 0;

          if (incomeDate) {
            totalIncomeYear = incomeDate.getFullYear();
          }

          if (this.endDate) {
            return totalIncomeYear - totalCurrentYear < this.delay;
          } else if (this.startDate) {
            return this.isDisabled ? ((totalIncomeYear - totalCurrentYear >= this.delay || totalTodayYear - totalCurrentYear < this.delay)) : (totalIncomeYear - totalCurrentYear > this.delay);
          } else {
            return totalTodayYear - totalCurrentYear < this.delay;
          }
        };
        break;

      default:
        this.disabledDate = (current: Date): boolean => {
          const today = new Date();
          let dayTime = 0;

          switch (delayType) {
            case 'MIN':
              dayTime = 1000 * 60;
              break;
            case 'H':
              dayTime = 1000 * 60 * 60;
              break;
            default:
              dayTime = 1000 * 60 * 60 * 24;
          }

          if (this.endDate) {
            const currentDate = new Date(new Date(current).setHours(0, 0, 0, 0));
            return incomeDate.getTime() - currentDate.getTime() < dayTime * this.delay;
          } else if (this.startDate) {
            const currentDate = new Date(new Date(current).setHours(23, 59, 59, 999));
            const delayDate = this.type === 'D' ? this.delay : (this.delay + 1);

            if (this.isDisabled) {
              // 如果存在时间选择，那么开始时间的日期，在结束控件是不需要禁选的
              return (incomeDate.getTime() - currentDate.getTime() > dayTime * delayDate) || (today.getTime() - currentDate.getTime() <= dayTime * this.delay);
            } else {
              return incomeDate.getTime() - currentDate.getTime() > dayTime * delayDate;
            }
          } else {
            const currentDate = new Date(new Date(current).setHours(0, 0, 0, 0));
            return today.getTime() - currentDate.getTime() < dayTime * this.delay;
          }
        };
        break;
    }

    this.setDisabledTime();
  }

  // 返回一个数据为从 start 到 end 的数组
  range(start: number, end: number, step = 1): number[] {
    const result: number[] = [];
    for (let i = start; i < end; i += step) {
      result.push(i);
    }
    return result;
  }

  // 设置禁选时间
  setDisabledTime() {
    const endDate = this.endDate || this.initialTime;

    if ((this.type === 'C' || this.type === 'MIN' || this.type === 'H') && this.showDateTime) {
      this.disabledDateTime = (current: Date) => {
        let disabledHoursArr = [];
        // 不可选的数值是时间粒度的倍数
        let disabledMinutesArr = [];
        let disabledSecondsArr = [];

        // current指的是选中的日期，但在没有选择日期之前，在这里是拿不到current的，因此没选日期就不用做禁选判断
        if (current) {
          // 禁选开始时间以前的时间
          if (this.startDate) {
            // 选中的日期与初始日期，同年同月时
            if (current.getFullYear() === this.startDate.getFullYear() && current.getMonth() === this.startDate.getMonth()) {
              // 同一天，计算不能选择的小时
              if (current.getDate() === this.startDate.getDate()) {
                this.showDateTime.nzFormat.indexOf('mm') >= 0 ?
                  disabledHoursArr = this.range(0, this.startDate.getHours()) :
                  disabledHoursArr = this.range(0, this.startDate.getHours() + 1);

                // 如果大于等于当前时间的小时，计算不能选择的分钟
                if ((current.getHours() >= this.startDate.getHours())) {
                  const timeList = this.range(0, 60, this.granularity);
                  const startIndex = timeList.indexOf(this.startDate.getMinutes());
                  disabledMinutesArr = timeList.filter(val => val % this.granularity !== 0);

                  this.showDateTime.nzFormat.indexOf('ss') >= 0 ?
                    disabledMinutesArr = disabledMinutesArr.concat(timeList.slice(0, startIndex)) :
                    disabledMinutesArr = disabledMinutesArr.concat(timeList.slice(0, startIndex + 1));

                  // 如果大于等于当前时间的分钟，计算不能选择的秒数
                  if ((current.getMinutes() >= this.startDate.getMinutes())) {
                    disabledSecondsArr = disabledSecondsArr.concat(this.range(0, this.startDate.getSeconds() + 1));
                  }
                }
              }
            }
          }

          // 禁选结束时间以后的时间
          if (this.endDate || this.isDisabled) {
            // 选中的日期与初始日期，同年同月时
            if (current.getFullYear() === endDate.getFullYear() && current.getMonth() === endDate.getMonth()) {
              // 同一天，计算不能选择的小时
              if (current.getDate() === endDate.getDate()) {
                disabledHoursArr = this.range(endDate.getHours() + 1, 24);

                // 如果大于等于当前时间的小时，计算不能选择的分钟
                if (current.getHours() >= endDate.getHours()) {
                  const timeList = this.range(0, 60, this.granularityType ? 1 : this.granularity);
                  const endIndex = timeList.indexOf(endDate.getMinutes());
                  disabledMinutesArr = disabledMinutesArr.concat(timeList.slice(endIndex + 1));

                  // 如果大于等于当前时间的分钟，计算不能选择的秒数
                  if (current.getMinutes() >= endDate.getMinutes()) {
                    disabledSecondsArr = disabledSecondsArr.concat(this.range(endDate.getSeconds() + 1, 60));
                  }
                }
              }
            }
          }
        }

        return {
          nzDisabledHours: () => disabledHoursArr,
          nzDisabledMinutes: () => disabledMinutesArr,
          nzDisabledSeconds: () => disabledSecondsArr
        };
      };
    }
  }

  /**
   * 时间日期格式化
   */
  transformDate(date, type: string) {
    let format = '';

    if (type.length < 4) {
      switch (type) {
        case 'MIN':
          format = 'yyyy-MM-dd HH:mm:00';
          break;
        case 'H':
          format = 'yyyy-MM-dd HH:00:00';
          break;
        case 'D':
          format = 'yyyy-MM-dd 00:00:00';
          break;
        case 'W':
          format = 'yyyy-MM-dd 00:00:00';
          break;
        case 'MON':
          format = 'yyyy-MM-01 00:00:00';
          break;
        case 'Q':
          format = 'yyyy-MM-dd 00:00:00';
          break;
        case 'Y':
          format = 'yyyy-01-01 00:00:00';
          break;
        default:
          format = 'yyyy-MM-dd HH:mm:00';
          break;
      }
    } else {
      format = type;
    }

    const curDate = new Date(date);
    const halfHour = curDate.getHours() > 12 ? curDate.getHours() - 12 : curDate.getHours();
    const o = {
      'M+': curDate.getMonth() + 1, // 月份
      'd+': curDate.getDate(), // 日
      'H+': curDate.getHours() >= 10 ? curDate.getHours() : '0' + curDate.getHours(), // 24小时
      'h+': halfHour >= 10 ? halfHour : '0' + halfHour, // 12小时
      'm+': curDate.getMinutes() >= 10 ? curDate.getMinutes() : '0' + curDate.getMinutes(), // 分
      's+': curDate.getSeconds() >= 10 ? curDate.getSeconds() : '0' + curDate.getSeconds(), // 秒
      'q+': Math.floor((curDate.getMonth() + 3) / 3), // 季度
      S: curDate.getMilliseconds() // 毫秒
    };
    if (/(y+)/.test(format)) {
      format = format.replace(RegExp.$1, (curDate.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (const k in o) {
      if (new RegExp('(' + k + ')').test(format)) {
        format = format.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
      }
    }
    return format;
  }

  // 计算周粒度的起止日期
  calculateWeekStartEndDate() {
    let week = this.date.getDay();
    week = week === 0 ? 7 : week;
    const startDate = new Date(new Date(this.date).setDate((this.date.getDate() - week + 1)));
    const endDate = new Date(new Date(this.date).setDate((this.date.getDate() + 7 - week)));
    return {
      startDate,
      endDate
    };
  }

  // 监听周粒度切换事件
  handleWeekChange() {
    const weekDate = this.calculateWeekStartEndDate();

    if (this.startDate) {
      this.date = weekDate.endDate;
    } else {
      this.date = weekDate.startDate;
    }
  }

  // 计算季粒度的起止日期
  calculateQuarterStartEndDate() {
    if (!this.date) {
      this.curDate = this.initDate ? new Date(this.initDate) : new Date();
      this.date = this.initDate ? new Date(this.initDate) : new Date();
      this.curDate = new Date(this.curDate.setFullYear(this.curDate.getFullYear() - this.delay));
      this.date = new Date(this.date.setFullYear(this.date.getFullYear() - this.delay));
    }
    const startDate = new Date(new Date().setFullYear(this.date.getFullYear()));
    const endDate = new Date(new Date().setFullYear(this.date.getFullYear()));

    switch (this.curQuarter) {
      case 'Q1':
        startDate.setMonth(0);
        startDate.setDate(1);
        endDate.setMonth(2);
        endDate.setDate(31);
        break;
      case 'Q2':
        startDate.setMonth(3);
        startDate.setDate(1);
        endDate.setMonth(5);
        endDate.setDate(30);
        break;
      case 'Q3':
        startDate.setMonth(6);
        startDate.setDate(1);
        endDate.setMonth(8);
        endDate.setDate(30);
        break;
      case 'Q4':
        startDate.setMonth(9);
        startDate.setDate(1);
        endDate.setMonth(11);
        endDate.setDate(31);
        break;
    }

    return {
      startDate,
      endDate
    };
  }

  // 展开时间选择事件
  openCalendar() {
    let format = '';
    let suffix = '';

    switch (this.type) {
      case 'C':
        format = 'yyyy-MM-dd HH:mm:ss';
        suffix = '';
        break;

      case 'MIN':
        format = 'yyyy-MM-dd HH:mm';
        suffix = ':00';
        break;

      case 'H':
        format = 'yyyy-MM-dd HH';
        suffix = ':00:00';
        break;

      case 'D':
        format = 'yyyy-MM-dd';
        suffix = ' 00:00:00';
        break;

      case 'W':
        format = 'yyyy-MM-dd';
        suffix = ' 00:00:00';
        break;

      case 'MON':
        format = 'yyyy-MM';
        suffix = '-01 00:00:00';
        break;

      case 'Q':
        format = 'yyyy-MM-dd';
        suffix = ' 00:00:00';
        break;

      case 'Y':
        format = 'yyyy';
        suffix = '-01-01 00:00:00';
        break;
    }

    if (this.type === 'R') {
      let flag = false;

      if (this.date) {
        this.date.forEach((date, index) => {
          if (this.curDate) {
            if (this.transformDate(this.curDate[index], format) !== this.transformDate(date, format)) {
              flag = true;
              this.curDate[index] = new Date(this.transformDate(date, format) + suffix);
            }
          } else {
            this.curDate = [];
            flag = true;
            this.curDate[index] = new Date(this.transformDate(date, format) + suffix);
          }
        });
      }

      if (flag) {
        this.returnPickerParam();
      }
    } else if (this.type === 'W') {
      if (this.date) {
        if (this.curDate !== this.transformDate(this.date, format)) {
          this.curDate = this.transformDate(this.date, format);
          this.returnPickerParam();
        }
      }
    } else if (this.type === 'Q') {

    } else {
      if (this.date) {
        if (this.transformDate(this.curDate, format) !== this.transformDate(this.date, format)) {
          this.curDate = new Date(this.transformDate(this.date, format) + suffix);
          this.returnPickerParam();
        }
      }
    }
  }

  // 监测时间变化事件
  handleDateChange(date) {
    // 当前时间减去倒推时间的差值
    const dateDiffMin = new Date(date.getTime() - 1000 * 60 * this.delay);
    const dateDiffHour = new Date(date.getTime() - 1000 * 60 * 60 * this.delay);
    const diffMin = dateDiffMin.getMinutes();
    const diffHour = dateDiffHour.getHours();

    switch (this.delayType || this.type) {
      case 'H':
        this.date = new Date(dateDiffHour.getTime() - 1000 * 60 * 60 * (diffHour % this.granularity));
        break;
      case 'MIN':
        this.date = new Date(dateDiffMin.getTime() - 1000 * 60 * (diffMin % this.granularity));
        break;
    }
  }

  // 点击日期控件输入框
  handleClickQuarter() {
    this.quarterOpen = true;
  }

  // 监测季度选择事件
  handleQuarterSelect(quarter, index) {
    this.curQuarter = quarter;
    this.quarterIndex = index;
  }

  // 监测季度确定事件
  handleQuarterConfirm() {
    const quarterDate = this.calculateQuarterStartEndDate();

    if (this.startDate) {
      this.curDate = quarterDate.endDate;
      this.date = quarterDate.endDate;
    } else {
      this.curDate = quarterDate.startDate;
      this.date = quarterDate.startDate;
    }
    this.returnPickerParam();
    this.quarterOpen = false;
  }

  // 返回时间参数
  returnPickerParam() {
    const paramObj = {
      date: this.rtDateFormat ? this.transformDate(this.curDate, this.rtDateFormat) : this.transformDate(this.curDate, this.format)
    };

    if (this.type === 'W') {
      const weekDate = this.calculateWeekStartEndDate();
      paramObj['startDate'] = weekDate['startDate'];
      paramObj['endDate'] = weekDate['endDate'];
    } else if (this.type === 'Q') {
      const quarterDate = this.calculateQuarterStartEndDate();
      paramObj['startDate'] = quarterDate['startDate'];
      paramObj['endDate'] = quarterDate['endDate'];
    }

    if (this.returnParam) {
      this.returnParam.split(',').forEach(param => {
        paramObj[param] = this[param];
      });
    }

    this.selectDate.emit(paramObj);
  }
}
